package com.endi.sap.dingding;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * DingDing的HttpClient创建公共方法
 */
@Component
public class CreateDingClient {

    @Value("${dingding.appKey}")
    private String appKey;

    @Value("${dingding.appToken}")
    private String appToken;

    private static final Logger logger = LoggerFactory.getLogger(CreateDingClient.class);

    public com.aliyun.dingtalkyida_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkyida_1_0.Client(config);
    }

    /**
     * 封装公共方法给API使用
     * @param accessToken
     * @return
     */
    public Map<String, Object> getClient(String accessToken){
        Map<String, Object> map = new HashMap<>();
        map.put("appKey",appKey);
        map.put("appToken",appToken);
        map.put("accessToken",accessToken);
        try {
            map.put("client",createClient());
        } catch (Exception e) {
            logger.info("客户端创建失败",e.getMessage());
            e.printStackTrace();
        }

        return map;
    }


}
